// 基本配置项 
import store from '../store/index.js'
const environment = 'development' //'formal'
// const environment = 'formal'
exports.baseUrl = environment == 'development' ? 'https://此处放域名/api' : 'https://此处放域名/api'
let imgUrl = environment == 'development' ? 'https://此处放域名/api' : 'https://此处放域名'
exports.shareImg = 'https://此处放域名/uploads/logo.png'
exports.h5url = environment == 'development'  ? 'https://此处放域名/api'  : 'https://此处放域名/h5/#/'
exports.originalId = 'gh_4699bb51804c'
exports.environment = environment
exports.imgUrl = imgUrl
exports.regexImg = function (value) {
  return /^(http|https):/.test(value) || /^(@)/.test(value) ? value : imgUrl + value
}
exports.onPreview = function (value) {
  return uni.previewImage({
    current: 0,
    urls: [/^(http|https):/.test(value) || /^(@)/.test(value) ? value : imgUrl + value],
  });
}
exports.onGetSystemInfo = function () {
  uni.getSystemInfo({
    success: (e) => {
      if (e && e.statusBarHeight) store.commit('setStatusBarHeight', e.statusBarHeight)
    }
  })
  return
}

exports.formatFloat = function (arg1, arg2, operation) {
  var r1, r2, m;
  try {
    r1 = arg1.toString().split(".")[1].length;
  } catch (e) {
    r1 = 0;
  }
  try {
    r2 = arg2.toString().split(".")[1].length;
  } catch (e) {
    r2 = 0;
  }
  m = Math.pow(10, Math.max(r1, r2));
  switch (operation) {
    case 1:
      return ((arg1 * m + arg2 * m) / m).toFixed(2);
      break;
    case 2:
      return ((arg1 * m - arg2 * m) / m).toFixed(2);
      break;
    case 3:
      return Number((arg1 * m * arg2 * m) / (m * m)).toFixed(2);
      break;
    case 4:
      return Number(arg1 / arg2).toFixed(2);
      break;
    default:
      break;
  }
}
exports.$editUploadBack = function (type, modular) { //增 删 查
  var uploadBack = store.state.uploadBack
  // store.commit('setUploadBack', [])
  if (type == 1) {
    if (uploadBack.indexOf(modular) == -1) {
      uploadBack.push(modular)
      store.commit('setUploadBack', uploadBack)
    }
  } else if (type == 2) {
    if (uploadBack.indexOf(modular) != -1) {
      uploadBack.splice(uploadBack.indexOf(modular), 1)
      store.commit('setUploadBack', uploadBack)
    }
  } else if (type == 3) {
    var flag = uploadBack.indexOf(modular) != -1 ? true : false
    if (flag) {
      uploadBack.splice(uploadBack.indexOf(modular), 1)
      store.commit('setUploadBack', uploadBack)
    }
    return flag
  } else if (type == 4) {
    if (!uploadBack || uploadBack.length == 0) return false
    let row = uploadBack.find(c => c.split('?')[0] == modular)
    if (row && row.split('?').length > 1) {
      var option = row.split('?')[1]
      var params = {}
      option.split('&').forEach(v => {
        var obj = {}
        params[v.split('=')[0]] = v.split('=')[1]
      })
      uploadBack.splice(uploadBack.indexOf(row), 1)
      store.commit('setUploadBack', uploadBack)
      return params
    } else {
      return false
    }

  }

}

exports.onOpenArea = function (latitude, longitude) {
  if (!latitude || !longitude) return uni.showToast({
    title: '获取位置失败',
    icon: 'none'
  })
  uni.openLocation({
    latitude: Number(latitude),
    longitude: Number(longitude),
    success: res => { },
    fail: err => {
      uni.showToast({
        title: '获取位置失败',
        icon: 'none'
      })
    }
  });
},
  exports.onMerchant = function (phone) {
    if (!phone) return uni.showToast({
      title: '获取联系方式失败',
      icon: 'none'
    })
    uni.makePhoneCall({
      phoneNumber: phone.toString(),
	  success:()=>{},
	  fail:(e)=>{}
    });
  }

let getUserInfo = function (url, must = 1) { // 1提示 2 否
  if (!store.state.token) {
    let routes = getCurrentPages()
    let curRoute = routes[routes.length - 1].route
    let curParam = routes[routes.length - 1].options
    let param = ''
    for (let key in curParam) {
      if (param == '') {
        param += '?' + key + '=' + curParam[key]
      } else {
        param += '&' + key + '=' + curParam[key]
      }
    }
    let urlPage = `/pagesLogin/index?to=/${encodeURIComponent(curRoute + param)}`
    if (must == 1) {
      uni.showModal({
        content: '此功能需要登录，是否即刻登录？',
        success: async (res) => {
          if (res.confirm) {
            return uni.navigateTo({
              url: urlPage
            })
          }
        }
      })
    } else {
      return uni.navigateTo({
        url: urlPage
      })
    }
  } else {
    return uni.navigateTo({
      url: url ? url : '/pages/index/index'
    })
  }
}
exports.getUserInfo = getUserInfo

exports.$chatOfferList = function (num, type, user, info) { //1增  2查  3 删除 4删除
  var offerList = JSON.parse(JSON.stringify(store.state.chat_offer_current))
  if (!offerList) return uni.showToast({ title: '操作错误', icon: 'none' })
  var index = type == 'thirdPartySupervision' ? 0 : type == 'findDesign' ? 1 : type == 'findCraftsman' ? 2 : Number(type) + 2
  if (num == 1) {
	  console.log(offerList,'报价js');
	  console.log(index,'报价jsindex');
    if (offerList[index].list) {
      let ishop = offerList[index].list.findIndex(v => v.user.id == user.id)
	  console.log(ishop,'报价js2');
      if (ishop != -1) {
        offerList[index].list[ishop].list.push(info)
      } else {
        offerList[index].list.push({ list: [info], user })
      }
	  console.log(offerList,'报价js3');
      store.commit('set_chat_offer_current', offerList)
    } else {
      offerList[index].list = [{ list: [info], user }]
	  console.log(offerList,'报价js3');
      store.commit('set_chat_offer_current', offerList)
    }
    uni.showToast({ title: '已添加到当前报价单', icon: 'none' })
  } else if (num == 2) {
    // return offerList[index].list && offerList[index].list.findIndex(v=>v.user.id == user.id) != -1
    if (offerList[index].list) {
      let ishop = offerList[index].list.findIndex(v => v.user.id == info.shopping_id)
      if (ishop != -1) {
        return offerList[index].list[ishop].list.findIndex(v => v.goods_id == info.id) != -1
      } else {
        return false
      }
      store.commit('set_chat_offer_current', offerList)
    } else {
      return false
    }

  } else if (num == 3) {
    if (!offerList[index].list) return
    let ishop = offerList[index].list.findIndex(v => v.user.id == user.id)
    if (ishop == -1) return
    uni.showLoading({ mask: true })
    offerList[index].list.splice(ishop, 1)
    if (offerList[index].list == 0) offerList[index].list = null
    store.commit('set_chat_offer_current', offerList)
    uni.hideLoading()
  } else {
    if (!offerList[index].list) return
    let ishop = offerList[index].list.findIndex(v => v.user.id == info.shopping_id)
    if (ishop == -1) return
    let isc = offerList[index].list[ishop].list.findIndex(v => v.goods_id == info.id)
    if (isc == -1) return
    uni.showLoading({ mask: true })
    offerList[index].list[ishop].list.splice(isc, 1)
    if (offerList[index].list == 0) offerList[index].list = null
    store.commit('set_chat_offer_current', offerList)
    uni.hideLoading()
  }
}

exports.$chatPages = function () {
  var routes = getCurrentPages() // 获取当前打开过的页面路由数组
  if (!routes) return ni.showToast({ title: '添加失败,请重新进入报价单', icon: 'none' })
  routes.reverse()
  var index = routes.findIndex(x => x.route == 'pagesServiceProvider/intersection/offerCurrent')
  if (index != -1) {
    uni.navigateBack({
      delta: index
    })
  } else {
    return uni.showToast({ title: '添加失败,请重新进入报价单', icon: 'none' })
  }
}

exports.$swiperPage = function (row) {
  if (!row.jump_id || row.type == 0) return
  switch (row.type) {
    case 1:
      uni.navigateTo({ url: `/pagesHome/richTextDetails?id=${row.jump_id}&type=swiperDetail` })
      break;
    case 2: //1工长2设计师3建材城4厂商5授权店
      if (row.shop_type == 1) {
        uni.navigateTo({ url: `/pagesCommodity/shopDetailsDefault?type=findDesign&id=${row.jump_id}` })
      } else if (row.shop_type == 2) {
        uni.navigateTo({ url: `/pagesCommodity/shopDetailsDefault?type=findCraftsman&id=${row.jump_id}` })
      } else {
        uni.navigateTo({ url: `/pagesCommodity/shopDetailsBuilding?type=buildingMaterialsCity&id=${row.jump_id}` })
      }
      break;
    case 3:
      uni.navigateTo({ url: `/pagesCommodity/information?id=${row.jump_id}&type=decorationMoneyExchange` })
      break;
    default:
      break;
  }
}
  exports.$onTip = function (params) {
	 let pages = getCurrentPages();
	 var beforePage = ''
	 if (pages.length >= 1) beforePage = pages[pages.length - 1]
	 if(beforePage && (!store.state.chat_to || !(store.state.chat_to.id == params.from && beforePage.route == 'pagesChat/chatRoom' ) ) && (beforePage.route != 'pagesChat/systemMessage' || beforePage.route != 'pagesChat/chatRoom')){
		if(beforePage.$vm.$refs.page) beforePage.$vm.$refs.page.onType(params)
   }
  }
